
<?php $baseUrl=Zend_Controller_Front::getInstance()->getBaseUrl(); ?>
<input type="hidden" id="baseUrl" value="<?php echo $baseUrl; ?>"/>
<input type="hidden" id="bid" value="<?php echo $this->bookDetail['_id'];?>"/>
<input type="hidden" id="uid" value="<?php echo $this->uid; ?>" />
<link rel="stylesheet" rev="stylesheet" href="<?php  echo $baseUrl; ?>/css/book_detail.css" type="text/css" media="all" />
<script type="text/javascript" src="<?php  echo $baseUrl; ?>/js/commonfunctions.js"></script>
<script type="text/javascript" language="javascript" src="<?php echo $baseUrl.'/tiny_mce/tiny_mce.js';?>"></script>
<script type="text/javascript" language="javascript">
 /*
    tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "pagebreak,style,layer,table,advhr,advimage,advlink,iespell,inlinepopups,insertdatetime,preview,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist",

		// Theme options
		theme_advanced_buttons1 : "styleselect,formatselect,fontselect,fontsizeselect,|,bold,italic,underline,strikethrough",
		theme_advanced_buttons2 : "justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,link,unlink,anchor,|,forecolor,backcolor",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
        */

     var baseUrl=$("#baseUrl").val();
    /*Ẩn hiện menu danh mục sách*/
    $(document).ready(function()
    {
        $('#categorybook h4').mouseenter(function()
        {
            $('#ul_category').css('display', 'block');
        }
        );

        $('#categorybook h4').mouseout(function()
        {
            $('#ul_category').css('display', 'none');
        }
        );

        $('#ul_category').mouseenter(function()
        {
            $('#ul_category').css('display', 'block');
        }
        );

        $('#ul_category li').mouseenter(function()
        {
            $('#ul_category').css('display', 'block');
        }
        );

        $('#ul_category li').mouseout(function()
        {
            $('#ul_category').css('display', 'none');
        }
        );
    }
    );

    /* Thay đổi style khi chọn một mục trong danh mục bên trái*/
    $(document).ready(function()
    {
        $('#body_main_left a').click(function()
        {
            var id=$(this).attr('id');
            $('#'+id).addClass('active');
            var arrAnchor=document.getElementById('body_main_left').getElementsByTagName('a');
            for(var i=0;i<arrAnchor.length;i++)
            {
                var idTemp="";
                for(var j=0;j<arrAnchor[i].attributes.length;j++)
                {
                    if(arrAnchor[i].attributes[j].name.toLowerCase()=="id")
                    {
                        idTemp=arrAnchor[i].attributes[j].value;
                        break;
                    }
                }
                if(id!=idTemp)
                {
                    $('#'+idTemp).removeClass('active');
                }
            }
        }
        );
        $("#urate").click(function(){

        // kiem tra dang nhap chua? chi dang nhap moi duoc rating


        //neu da dang nhap
            var url = $("#baseUrl").val() + "/default/book/rate/bid/" + $("#bid").val() +"?rating="+$("input:radio[name=urating]:checked").val();
            //alert($("input:radio[name=urating]:checked").val());

           $.ajax({
           type : 'GET',
            url:url,
            beforeSend: function(){
            //alert("send");
               },
            success : function(res){

                res=parseInt(StandardString(res.toString()));
                //alert(res);
                if(res==1){
                    alert("Cảm ơn bạn đã đánh giá");
                }
                else if(res == -1) alert("Hãy đăng nhập để đánh giá sách");
                else
                    alert("Không đánh giá được!");
            }
           });
        });
    }
    );

    //var baseUrl=$('#baseUrl').val();
    /* ajax cho load nội dung các phần: sách cùng tác giả*/
    $(document).ready(function()
    {
        setCurrentPageBookSameAuthor(0);
        setCurrentPageBookSameCat(0);
    }
    );

    var currentPageBookSameCat=1;
    function setCurrentPageBookSameCat(value)
    {
        currentPageBookSameCat +=value;
        if(currentPageBookSameCat <=0)
            currentPageBookSameCat=5;
        if(currentPageBookSameCat>5)
            currentPageBookSameCat=1;

        $('#pagebookbycat').html('trang '+currentPageBookSameCat+'/5');
        loadBookSameCat(3,currentPageBookSameCat);
    }
    function loadBookSameCat(pagesize,currentpage)
    {
         var content="<table width='100%'><tr>\n\
            <td><a><img src='"+baseUrl+"/images/arrow1.png' alt=''/></a></td>\n\
            <td><img src='"+baseUrl+"/images/loading.gif"+"'/></td>\n\
            <td><img src='"+baseUrl+"/images/loading.gif"+"'/></td>\n\
            <td><img src='"+baseUrl+"/images/loading.gif"+"'/></td>\n\
            <td><a><img src='"+baseUrl+"/images/arrow2.png' alt='' /></a></td></tr><table>";
        $('#bookByCat').html(content);
        var idCat=$('#idCat').val();
         $.ajax({
              type:'GET',
              async: true,
              cache: false,
              timeout: 30000,
              url: baseUrl + '/default/ajax/bookbycat?idcat='+idCat+'&current='+currentpage+'&pagesize='+pagesize,
              success:function(data)
              {
                if(data!="")
                {
                    $('#bookByCat').html(data);
                }
                else
                {

                }
              }
        });
    }


    var currentPageBookSameAuthor=1;
    function setCurrentPageBookSameAuthor(value)
    {
        var idAuthor=$('#idAuthor').val();
        var numberBook=0;
        $.ajax({
              type:'GET',
              async: true,
              cache: false,
              timeout: 30000,
              url: baseUrl + '/default/ajax/numberpagebooksameauthor?idAuthor='+idAuthor,
              success:function(data)
              {
                if(data!="")
                {
                    numberBook=parseInt(data);
                    if(numberBook>0)
                    {
                        var maxpage=parseInt(numberBook/3);
                        if(numberBook%3 != 0)
                            maxpage++;
                        currentPageBookSameAuthor +=value;
                        if(currentPageBookSameAuthor<=0)
                            currentPageBookSameAuthor=maxpage;
                        if(currentPageBookSameAuthor > maxpage)
                            currentPageBookSameAuthor=1;

                        if(numberBook>3)
                            $('#pageBookByAuthor').html('trang '+currentPageBookSameAuthor +'/'+maxpage);
                        loadBookSameAuthor(3,currentPageBookSameAuthor);
                    }
                }
                else
                {}
              }
        });
    }
    
    function loadBookSameAuthor(pagesize,currentpage)
    {
        var content="<table width='100%'><tr>\n\
            <td><a><img src='"+baseUrl+"/images/arrow1.png' alt='' /></a></td>\n\
            <td><img src='"+baseUrl+"/images/loading.gif"+"'/></td>\n\
            <td><img src='"+baseUrl+"/images/loading.gif"+"'/></td>\n\
            <td><img src='"+baseUrl+"/images/loading.gif"+"'/></td>\n\
            <td><a><img src='"+baseUrl+"/images/arrow2.png' alt='' /></a></td></tr><table>";

        $('#bookByAuthor').html(content);

        var idAuthor=$('#idAuthor').val();
         $.ajax({
              type:'GET',
              async: true,
              cache: false,
              timeout: 30000,
              url: baseUrl + '/default/ajax/bookbyauthor?idAuthor='+idAuthor+'&pagesize='+pagesize+'&current='+currentpage,
              success:function(data)
              {
                if(data!="")
                {
                    $('#bookByAuthor').html(data);
                }
                else
                {}
              }
        });
    }

    function postComment()
    {        
        //if(StandardString(tinyMCE.get('new_comment').getContent()).toString().length==0)
        if(StandardString($('#new_comment').val()).toString().length==0)
        {
            alert("Chưa nhập nội dung chia sẻ");
            return;
        }
        
        //var content=StandardString(tinyMCE.get('new_comment').getContent());
        var content=StandardString($('#new_comment').val()).toString();
        var urlDes = encodeURI(baseUrl + '/default/ajax/newcomment');//?uid='+$('#uid').val()+'&bid='+$('#bid').val()+'&content='+content);
        $.ajax({
              type:'POST',
              //async: true,
              //cache: false,
              data:{
                  'uid':$('#uid').val(),
                  'bid':$('#bid').val(),
                  'content':content
              },
              timeout: 30000,
              url: urlDes ,
              beforeSend: function(){
                
              },
              success:function(data)
              {
                if(data!="")
                {
                    //tinyMCE.get('new_comment').setContent(data);
                    $('#new_comment').val("");
                    var tbcomment=$('#tbcomment').html();
                    $('#tbcomment').html(tbcomment + data);
                }
                else
                {
                    alert("Không thêm được bình luận!");
                }
              }
        });
    }

    function addBookCart()
    {
        var url = baseUrl + "/default/ajax/addcart";
        var uid=StandardString($('#uid').val()).toString();
        if(uid.length==0)
            uid='0';

        $.ajax({
           type : 'POST',
           timeout: 3000,
           data:{
                  'uid':uid.toString(),
                  'bid':$('#bid').val(),
                  'number':$('#number_buy option:selected').val()
           },
           url : url,
           beforeSend: function(){
               
           },
           success: function(res)
           {
               //alert(res);
               //alert(res.toString());
               res=StandardString(res.toString());
               res=parseInt(res);
               if(res==1)
               {
                   alert("Thêm sách vào giỏ thành công!");
               }
               if(res==-1)
               {
                   alert("Không thêm được sách vào giỏ!")
               }
               if(res==2)
               {
                   alert("Bạn đang dùng chế độ người dùng thường. Sách trong giỏ chỉ được lưu 1 tiếng. Hãy đăng ký để lưu trữ sách trong giỏ vô thời hạn và nhiều tiện ích khác!");
               }
           }
        });
    }
</script>
<div id="body_main">
    <div id="body_main_left">
        <ul>
            <li>
                <a id="menu1" href="#top" class="active">Sách đang xem</a>
            </li>
            <li>
                <a id="menu2" href="#detailInfor">Thông tin chi tiết</a>
            </li>
            <li>
                <a id="menu3" href="#rating">Khách hàng đánh giá</a>
            </li>
            <li>
                <a id="menu4" href="#comment">Khách hàng cảm nhận</a>
            </li>
            <li>
                <a id="menu5" href="#same_author">Sách cùng tác giả</a>
            </li>
            <li>
                <a id="menu6" href="#same_category">Sách cùng chủ đề</a>
            </li>
            <li>
                <a id="menu7" href="#both_buy">Sách được mua cùng</a>
            </li>
        </ul>
    </div>
    <div id="body_main_middle">
        <div class="header"  id="top">
            <h4>
                <a href="<?php echo $baseUrl; ?>">Trang chủ</a>
                <span class="separate">&gt;</span>
                <a href="<?php echo $baseUrl .'/book/index/type/'.$this->bookDetail["id_category"] ; ?>"><?php echo $this->bookDetail["catname"]; ?></a>
            </h4>
        </div>
        <div>
            <table width="100%" cellspacing="0" cellpadding="5">
                <tr>
                    <td width="300" class="cover"  style="text-align: center;">
                        <img class="cover" src="<?php echo $this->bookDetail["image"]; ?>" alt=""/><br/><br/>
                        <span class="rating">
                        <?php
                            if($this->bookDetail["sumrating"]==2)
                                    echo '<img alt="" src="'. $baseUrl .'/images/stars-2-0.gif"/>' ;
                            if($this->bookDetail["sumrating"]==3)
                                    echo '<img alt="" src="'. $baseUrl .'/images/stars-3-0.gif"/>' ;
                            if($this->bookDetail["sumrating"]==4)
                                    echo '<img alt="" src="'. $baseUrl .'/images/stars-4-0.gif"/>' ;
                            if($this->bookDetail["sumrating"]==5)
                                    echo '<img alt="" src="'. $baseUrl .'/images/stars-5-0.gif"/>' ;
                        ?>
                    </span><br/>
                    <?php
                        if((int)$this->bookDetail["view"] >0)
                        {
                            echo '<span class="view">'.$this->bookDetail["view"].'  </span> <span>người đã xem</span><br/>';
                        }
                    ?>
                    </td>
                    <td width="70%" class="detail">
                        <span class="namebook">
                            <?php echo $this->bookDetail["name"];?>
                        </span><br/>
                        Tác giả: <a><?php echo $this->bookDetail["authorname"]; ?></a><br/>
                        Nhà xuất bản: <?php echo $this->bookDetail["publisher"]; ?>.
                        Xuất bản năm: <?php echo $this->bookDetail["date_publish"]; ?><br/><br/>
                         <?php
                            if(((int)$this->bookDetail["saleoff"])>0)
                            {
                                echo "<span class='oldprice'>Giá gốc:".$this->bookDetail["cost"]." đ</span><br/>";
                                echo "<span class='price'>Giá bán:".($this->bookDetail["cost"]*(100-$this->bookDetail["saleoff"])/100)." đ</span>";
                            }
                            else
                                echo "<span class='price'>Giá bán:".$this->bookDetail["cost"]." đ</span>";
                        ?>
                        <br/><br/>
                        <!--
                        <a class="button" href="<?php echo $baseUrl .'/default/index/purchase/idBook/'.$this->bookDetail['_id']; ?>">
                            Mua
                        </a>
                        <a class="button" href="#">
                            Tặng
                        </a> -->
                        <div style="width:103px; height: 63px; padding: 10px; border: solid 1px #9baaae; text-align: center;">
                            Số lượng
                            <select id="number_buy" name="number_buy" >
                                <?php
                                    for($i=1;$i<=10;$i++)
                                    {
                                        echo '<option value="'.$i.'">'.$i.'</option>';
                                    }
                                ?>
                            </select><br/><br/>
                            <a class="button" id="push_cart" onclick="return addBookCart();">
                                Thêm vào giỏ
                            </a>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
        <div class="clear"></div>
        <div class="header" id="detailInfor">
            <h4>Thông tin chi tiết</h4>
        </div>
        <div>
            <?php echo $this->bookDetail["information"]; ?>
        </div>
        <div class="clear"></div>

        <div class="header" id="rating">
            <h4>Khách hàng đánh giá</h4>
        </div>
        <div>
            Bạn đánh giá
            <form action="">
                <input type="radio" name="urating" value="1" checked="yes"/> Nên xem <br>
                <input type="radio" name="urating" value="2"/> Nên đọc <br>
                <input type="radio" name="urating" value="3"/> Hay <br>
                <input type="radio" name="urating" value="4"/> Thích <br>
                <input type="radio" name="urating" value="5"/> Rất thích <br>
            </form>
            <button id="urate">Đánh giá</button>
        </div>
        
        <div class="clear"></div>

        <div class="header" id="comment">
            <h4>Khách hàng cảm nhận</h4>
        </div>
        <div>
            <div id="content_comment">
                <table class="comment" id="tbcomment" width="100%" cellspacing="0" cellpadding="5px">
                 <?php
                    if(isset ($this->comments))
                    {
                        foreach($this->comments as $comment)
                        {
                  ?>
                    <tr id="<?php echo 'tr'.$comment["stt"]; ?>">
                        <td width="80px">
                            <img class="avatar" alt="" src="<?php echo $comment["user_image"];?>" /><br/><br/>
                            <span style="font-weight: bold; display:inline-block;float:left; padding:0 3px;">
                                <?php echo $comment["user_name"];?>
                            </span>
                        </td>
                        <td>
                           <?php echo $comment["content"]; ?>
                           <br/><br/>
                           <?php echo 'Bình luận vào '.$comment["date"]; ?>
                            <?php
                                if(isset($comment["like"]))
                                {
                                    echo '&nbsp;&nbsp;';
                                    echo 'Có <span id="numberlike" class="highlight">'. count($comment["like"]).'</span> người thích. ';
                                }
                                if($this->username !=null && $this->uid!=$comment["id_user"] )
                                {
                                    $liked=false;
                                    if(isset($comment["like"]) && is_array($comment["like"]))
                                    {
                                        for($i=0;$i<count($comment["like"]);$i++)
                                        {
                                            if($comment["like"][$i]==$this->uid)
                                            {
                                                $liked=true;
                                                break;
                                            }
                                        }
                                    }
                                    if(!$liked)
                                    {
                                        echo '&nbsp;&nbsp;';
                                        echo '<a id="like'.$comment["stt"].'" class="menu" id="like">Thích</a>';
                                    }
                                }
                            ?>
                            <?php
                                if($this->username !=null && $this->uid ==$comment["id_user"])
                                {
                                    echo '&nbsp;&nbsp;';
                                    echo '<a id="delete'.$comment["stt"].'" class="menu">Xóa</a>';
                                }
                            ?>
                        </td>
                    </tr>
                  <?php
                        }
                    }
                 ?>
                </table>
                <div></div>
            </div>
            <?php
                if(count($this->comment)>20)
                {
            ?>
                
            <?php
                }
            ?>
            <div class="clear"></div>
            <div>
                <div>
                    <?php
                        if($this->username ==null)
                        {
                    ?>
                        <a class="menu" href="<?php echo $baseUrl .'/default/index/login' ?>"> Đăng nhập</a>
                        để cùng chia sẻ.<br />
                    <?php
                        }
                        else
                        {
                    ?>
                        Viết cảm nhận và cùng chia sẻ với mọi người<br/>
                    <?php
                        }
                    ?>
                </div>
                <br/>
                <textarea rows="8" id="new_comment" name="new_comment" style="width: 100%; border:solid 1px #9baaae;">
                </textarea>
                <br/>
                 <?php
                        if($this->username !=null)
                        {
                            echo '<input onclick="postComment()" type="button" id="post" name="post" value="Chia sẻ" style="float:right;"/>';
                        }
                 ?>

            </div>
        </div>
        <div class="clear"></div>

        <div class="header" id="same_author">
            <h4>Sách cùng tác giả</h4>
        </div>
        <div id="pageBookByAuthor" class="currentpage"></div>
        <div class="clear1"></div>
        <div id="bookByAuthor">
            <!-- ajax for loading content -->
        </div>
        <input id="idAuthor" type="hidden" value="<?php echo $this->bookDetail["id_author"]; ?>"/>
        <div class="clear"></div>

        <div class="header" id="same_category">
            <h4>Sách cùng chủ đề</h4>
        </div>
        <div id="pagebookbycat" class="currentpage">trang 1/5</div>
        <div class="clear1"></div>
        <div id="bookByCat">
            <!-- ajax for loading content -->
        </div>
        <input id="idCat" type="hidden" value="<?php echo $this->bookDetail["id_category"] ?>"/>
        <div class="clear"></div>

        <div class="header" id="both_buy">
            <h4>Sách được mua cùng</h4>
        </div>
        <div>

        </div>
        <div class="clear"></div>
    </div>
</div>
<div class="clear"></div>